//关键字
var keyword = getParmas(location.search).keyword;
//当前页码
var page = 1;
//排序规则-1升序，2降序
var price = 1
var num = 1;
//传递的数据
var data = {
    proName: keyword,
    page: page,
    pageSize: 3
}
$(function () {
    //下拉加载初始化
    mui.init({
        pullRefresh: {
            container: '#pullrefresh',
            up: {
                auto: true,
                contentrefresh: '正在加载...',
                callback: getData
            }
        }
    });
    //价格排序
    $('.priceSort').on('tap', function () {
        //判断当前是排序还是降序
        price = price == 1 ? 2 : 1;
        //点击价格排序传入价格排序规则
        data.price = price;
        dataSort()
    })
    //销量排序
    $('.numSort').on('tap', function () {
        //判断当前是排序还是降序
        num = num == 1 ? 2 : 1;
        //点击销量排序传入销量排序规则
        data.num = num;
        dataSort()
    })
})
//筛选
function dataSort() {
    //重置页码
    page = 1;
    //清空页面
    var table = document.body.querySelector('.items');
    table.innerHTML = '';
    //重置加载
    mui('#pullrefresh').pullRefresh().refresh(true);
    //重新获取数据
    getData();
}
//获取数据
function getData() {
    data.page = page++;
    $.ajax({
        type: "get",
        url: "/product/queryProduct",
        data,
        success: function (res) {
            console.log(res);
            if (res.data.length > 0) {
                var table = document.body.querySelector('.items');
                for (var i = 0; i < res.data.length; i++) {
                    var li = document.createElement('li');
                    li.innerHTML = `<a href="detail.html?id=${res.data[i].id}">
                                        <img src="${res.data[i].pic[0].picAddr}" alt="">
                                        <span class="name">${res.data[i].proName}</span>
                                        <p>
                                            <span>￥${res.data[i].price}</span>
                                            <span><del>￥${res.data[i].oldPrice}</del></span>
                                        </p>
                                        <span class="buy-now">立即购买</span>
                                    </a>`;
                    table.appendChild(li);
                }
                mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
            } else {
                mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
            }
        }
    });
}
